package com.mz.servlet.sys;

import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import com.mz.base.util.ATTSystem;
import com.mz.entity.SysMenu;
import com.mz.entity.SysUsers;
import com.mz.service.sys.SysMenuService;
import com.mz.service.sys.SysRoleService;
import com.mz.servlet.CommonServlet;

public class SysMenuServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		String operating = request.getParameter("operating");
		if(StringUtils.isEmpty(operating)){
			//查询系统菜单列表信息
			this.findSysMenu(request, response);
		}else if("loadData".equals(operating)){
			//初始化页面数据
			this.loadData(request, response);
		}else if("save".equals(operating)){
			//保存系统菜单信息
			this.saveSysMenu(request, response);
		}else if("get".equals(operating)){
			//获取系统菜单信息
			this.getSysMenuById(request, response);
		}else if("delete".equals(operating)){
			//删除系统菜单信息
			this.deleteSysMenu(request, response);
		}
	}
	/**
	 * 删除系统菜单信息
	 * @param request
	 * @param response
	 */
	private void deleteSysMenu(HttpServletRequest request, HttpServletResponse response) {
		String sMenuId = request.getParameter("menuId");
		Long menuId = null;
		if(!StringUtils.isEmpty(sMenuId)){
			menuId = Long.valueOf(sMenuId);
		}
		
		JSONObject retJson = new JSONObject();
		retJson.put("code", 0);
		if(menuId==null){
			retJson.put("msg", "menuId is null");
		}else{
			SysUsers sessionSysUser = (SysUsers) request.getSession().getAttribute(ATTSystem.KEY_SESSION_LOGIN);
			SysMenuService sysMenuService = new SysMenuService();
			int row = sysMenuService.deleteSysMenu(menuId, sessionSysUser.getUserId());
			if(row <= 0){
				retJson.put("msg", "删除失败");
			}else{
				retJson.put("code", 1);
			}
		}
		
		CommonServlet.writer(response, retJson.toString());
	}

	/**
	 * 获取系统菜单信息
	 * @param request
	 * @param response
	 */
	private void getSysMenuById(HttpServletRequest request, HttpServletResponse response) {
		String sMenuId = request.getParameter("menuId");
		Long menuId = null;
		if(!StringUtils.isEmpty(sMenuId)){
			menuId = Long.valueOf(sMenuId);
		}
		
		JSONObject retJson = new JSONObject();
		retJson.put("code", 0);
		if(menuId==null){
			retJson.put("msg", "menuId is null");
		}else{
			SysMenuService sysMenuService = new SysMenuService();
			JSONObject jsonSysMenu = sysMenuService.getSysMenuById(menuId);
			if(jsonSysMenu==null){
				retJson.put("msg", "no existent menuId="+menuId+" The row.");
			}else{
				retJson.put("code", 1);
				retJson.put("data", jsonSysMenu);
			}
		}
		
		CommonServlet.writer(response, retJson.toString());
	}

	/**
	 * 保存系统菜单信息
	 * @param request
	 * @param response
	 */
	private void saveSysMenu(HttpServletRequest request, HttpServletResponse response) {
		String sMenuId = request.getParameter("menuId");
		String menuName = request.getParameter("menuName");
		String url = request.getParameter("url");
		String parameter = request.getParameter("parameter");
		String target = request.getParameter("target");
		String sOrderNum = request.getParameter("orderNum");
		String superMenuCode = request.getParameter("superMenuCode");
		
		Long menuId = null;
		Integer orderNum = null;
		if(!StringUtils.isEmpty(sMenuId)){
			menuId = Long.valueOf(sMenuId);
		}
		if(!StringUtils.isEmpty(sOrderNum)){
			orderNum = Integer.valueOf(sOrderNum);
		}
		
		SysUsers sessionSysUser = (SysUsers) request.getSession().getAttribute(ATTSystem.KEY_SESSION_LOGIN);
		
		Date date = new Date();
		SysMenu sysMenu = new SysMenu(menuId, superMenuCode, menuName, url, parameter, target, orderNum, 1,
				date, sessionSysUser.getUserId(), date, sessionSysUser.getUserId());
		SysMenuService sysMenuService = new SysMenuService();
		int rows = sysMenuService.saveSysMenu(sysMenu);
		
		JSONObject retJson = new JSONObject();
		if(rows==1){
			retJson.put("code", 1);
		}else{
			retJson.put("code", 0);
			retJson.put("msg", "保存失败");
		}
		CommonServlet.writer(response, retJson.toString());
		
	}

	/**
	 * 初始化页面数据
	 * @param request
	 * @param response
	 */
	private void loadData(HttpServletRequest request, HttpServletResponse response) {
		SysRoleService sysRoleService = new SysRoleService();
		JSONArray roleJsonArray = sysRoleService.findSysRole();
		CommonServlet.writer(response, roleJsonArray.toString());
	}

	/**
	 * 查询系统菜单列表
	 * @param request
	 * @param response
	 */
	private void findSysMenu(HttpServletRequest request, HttpServletResponse response) {
		SysMenuService sysMenuService = new SysMenuService();
		JSONArray sysMenuArray = sysMenuService.findSysMenu();
		
		CommonServlet.writer(response, sysMenuArray.toString());
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		this.doPost(request, response);
	}

	public void init() throws ServletException {}
	
	public SysMenuServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}
}